package com.zdkj.subway.check.annotation;

import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;

/**
 * @author cooze
 * @version 1.0.0 创建于 2020/10/24
 **/
public abstract class SwCheckNoRepeatSubmitBaseController extends BaseController {

    /**
     * 获取幂等Key
     *
     * @param uid
     * @param type
     * @param uniqueKey
     * @return
     */
    protected String getIdeKey(int uid, String type, String uniqueKey) {
        return String.format("subway:swcheck:check:%d:%s:%s", uid, type, uniqueKey);
    }


    /**
     * 判断是否重复提交
     * @param uid
     * @param type
     * @param uniqueKey
     * @return
     */
    protected boolean noPass(int uid, String type, String uniqueKey) {
        String ideKey = getIdeKey(uid, type, uniqueKey);
        if (redisTemplate().hasKey(ideKey)) {
            return true;
        }
        // 默认25秒内同一用户同一个地址同一个参数，视为重复提交
        redisTemplate().opsForValue().set(ideKey, "1", 25, TimeUnit.SECONDS);
        return false;
    }


    public abstract RedisTemplate<String, String> redisTemplate();


}
